const 
	DIGITS : array[1..36] of char = ('0', '1', '2', '3', '4', '5', '6', '7', '8',
																	 '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
																	 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
																	 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

function PToDec(p : longint; n : string) : int64;

var 
	current, step, index : int64; 
	i, j : longint;
	
begin
	step := 1;
	current := 0;
	for i := length(n) downto 1 do begin
		index := 1;
		for j := 1 to 36 do 	
			if digits[j] =n[i] then index := j;
		current := current + step * (index - 1);
		step := step * p;
	end;
	ptodec := current;
end;